package cn.lvhang.org.controller;

import cn.lvhang.basic.exception.BusinessException;
import cn.lvhang.basic.util.AjaxResult;
import cn.lvhang.basic.util.PageList;
import cn.lvhang.org.domain.Shop;
import cn.lvhang.org.query.ShopQuery;
import cn.lvhang.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController // @Controller +@ResponseBody
@RequestMapping("/shop")
@Api(tags = "商铺接口",description = "商铺接口详细描述")//用于对swagger字段的描述
public class ShopController {
    @Autowired
    private IShopService shopService;
    //新增或修改
    @PutMapping
    @ApiOperation(value = "商铺添加或修改",notes = "如果有id就是修改否则就是添加")//用于对swagger字段的描述
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if(shop.getId() == null){
                shopService.add(shop);
            }else{
                shopService.update(shop);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("操作失败！");
        }

    }
    //删除
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("操作失败！");
        }
    }
    //查询单个
    @GetMapping("/{id}")
    public Shop queryById(@PathVariable("id") Long id){
        return shopService.queryById(id);
    }
    //查询所有
    @GetMapping
    public List<Shop> queryAll(){
        return shopService.queryAll();
    }

    //分页 + 高级查询
    // @RequestBody接收前台发送的json字符串
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query){
        return shopService.queryPage(query);
    }
    //批量删除
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            shopService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("操作失败！");
        }
    }
    //店铺入驻
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (BusinessException e) {//自定义的业务异常
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {//系统级别的错误
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙，请稍后再试！");
        }
    }
}
